import { ref, watchEffect } from "vue";

export const useLocalStorage = (key, defaultValues) => {
	let initValue;

	try {
		initValue = JSON.parse(window.localStorage.getItem(key));
	} catch {
		//
	}
	const value = ref(initValue === undefined ? defaultValues : initValue);

	watchEffect(() => {
		const v = JSON.stringify(value.value);
		window.localStorage.setItem(key, v);
	});

	return value;
};
